package com.mallcloud.mall.mall.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.mall.api.entity.MallUserInfoModel;
import com.mallcloud.mall.mall.service.MallUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "mall-注册用户")
@RestController
@RequiredArgsConstructor
@RequestMapping("/mall/userInfo")
public class MallUserInfoController {
	private final MallUserInfoService mallUserInfoService;

	@ApiOperation("分页")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('mall_userinfo_view')")
	public R<IPage<MallUserInfoModel>> page(Page page, MallUserInfoModel mallUserInfo) {
		return R.ok(mallUserInfoService.page(page, Wrappers.query(mallUserInfo)));
	}

	@ApiOperation("查询详情")
	@GetMapping("getInfo")
	@PreAuthorize("@pms.hasPermission('mall_userinfo_view')")
	public R<MallUserInfoModel> getInfo(@RequestParam Long id) {
		return R.ok(mallUserInfoService.getById(id));
	}

	@ApiOperation("新增")
	@PostMapping("/save")
	@PreAuthorize("@pms.hasPermission('mall_userinfo_add')")
	public R<Boolean> save(@RequestBody MallUserInfoModel mallUserInfo) {
		return R.ok(mallUserInfoService.save(mallUserInfo));
	}

	@ApiOperation("修改")
	@PostMapping("/update")
	@PreAuthorize("@pms.hasPermission('mall_userinfo_edit')")
	public R<Boolean> updateById(@RequestBody MallUserInfoModel mallUserInfo) {
		return R.ok(mallUserInfoService.updateById(mallUserInfo));
	}

	@ApiOperation("删除")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('mall_userinfo_del')")
	public R<Boolean> removeById(@PathVariable String id) {
		return R.ok(mallUserInfoService.removeById(id));
	}

	@ApiOperation("批量删除")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('mall_userinfo_del')")
	public R<Boolean> removeByIds(@RequestBody String[] ids) {
		return R.ok(mallUserInfoService.removeBatchByIds(CollUtil.toList(ids)));
	}

	@ApiOperation("导出")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('mall_userinfo_export')")
	public List<MallUserInfoModel> export(MallUserInfoModel mallUserInfo) {
		return mallUserInfoService.list(Wrappers.query(mallUserInfo));
	}
}
